//
//  YQTabBarController.swift
//  yq微信
//
//  Created by yq on 2017/8/24.
//  Copyright © 2017年 yq. All rights reserved.
//

import UIKit

class YQTabBarController: UITabBarController,EMContactManagerDelegate {
   
    override func viewDidLoad() {
        super.viewDidLoad()
        
        EMClient.shared().contactManager.add(self, delegateQueue: nil)
    }
    
    // 接口：快速获取当前控制器
    class func yq_tabBarController() -> YQTabBarController {
     
        let sb = UIStoryboard.init(name: "Main", bundle: Bundle.main)
        let tabbar = sb.instantiateViewController(withIdentifier: "yqtabbar") as! YQTabBarController
        return tabbar
    }
    
    // 接口：接收到好友添加申请(弹出alertController)
    func friendRequestDidReceive(fromUser aUsername: String!, message aMessage: String!) {
        let ac = UIAlertController(title: "\(aUsername!)请求添加您为好友", message: "是否同意添加好友请求?", preferredStyle: .actionSheet)
        // 添加action
        ac.addAction(UIAlertAction(title: "同意", style: .default, handler: { (action ) in
           // 同意添加好友
            EMClient.shared().contactManager.approveFriendRequest(fromUser: aUsername, completion: { (userName , error ) in
                if error == nil {
                    print("发送同意添加好友 成功")
                }
                
                // 显示当前vc
//                for vc in self.viewControllers! {
//                    if vc.navigationItem.title == "通讯录" {
//                        self.selectedViewController = vc
//                    }
//                }
             })
        }))
       
        ac.addAction(UIAlertAction(title: "拒绝", style: .cancel, handler: { (action ) in
            // 拒绝好友请求
            let error = EMClient.shared().contactManager.declineInvitation(forUsername: aUsername)
            if (error == nil) {
                print("发送拒绝添加好友 成功")
            }
        }))
        
        self.present(ac, animated: true, completion: nil)
    }
}
